package com.llkj.contractmanagement.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.llkj.contractmanagement.R;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

public class ImageLoaderUtil {
    // 单利模式
    private static DisplayImageOptions options, headerOptions;

    // 实现了线程安全，又避免了同步带来的性能影响。
    public static DisplayImageOptions getOptions() {
        if (options == null) {
            synchronized (DisplayImageOptions.class) {
                if (options == null) {
                    options = new DisplayImageOptions.Builder()
                            .bitmapConfig(Bitmap.Config.RGB_565)
                            .showImageOnLoading(R.mipmap.ic_launcher)
                            .showImageForEmptyUri(R.mipmap.ic_launcher)
                            .showImageOnFail(R.mipmap.ic_launcher)
                            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                            .cacheInMemory(true).cacheOnDisk(true)
                            .considerExifParams(true).build();
                }
            }
        }
        return options;
    }

    // 实现了线程安全，又避免了同步带来的性能影响。
    public static DisplayImageOptions getHeaderOptions() {
        if (headerOptions == null) {
            synchronized (DisplayImageOptions.class) {
                if (headerOptions == null) {
                    headerOptions = new DisplayImageOptions.Builder()
                            .bitmapConfig(Bitmap.Config.RGB_565)
                            .showImageOnLoading(R.mipmap.ic_launcher)
                            .showImageForEmptyUri(R.mipmap.ic_launcher)
                            .showImageOnFail(R.mipmap.ic_launcher)
                            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                            .cacheInMemory(true).cacheOnDisk(true)
                            .considerExifParams(true).build();
                }
            }
        }
        return headerOptions;
    }

    /**
     * 初始化iml 配置
     *
     * @param context
     */
    public static void initConfig(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context)
                .memoryCacheExtraOptions(480, 800)
                .threadPoolSize(3)
                // .推荐1-5
                // 线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
                // You can pass your own memory cache
                // implementation/你可以通过自己的内存缓存实现
                .memoryCacheSize(2 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheExtraOptions(480, 320, null)
                // 自定义缓存路径
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                // 将保存的时候的URI名称用MD5 加密
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                // 自定义缓存路径
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .imageDownloader(
                        new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout
                .writeDebugLogs() // Remove for release app
                .build();// 开始构建
        ImageLoader.getInstance().init(config);
    }

    public static void displayImage(String url, ImageView iv) {
        try {
            ImageLoader.getInstance().displayImage(url, iv, getOptions());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void displayHeaderImage(String url, ImageView iv) {
        try {
            ImageLoader.getInstance().displayImage(url, iv, getHeaderOptions());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * uil 加载本地图片
     */
    public static String getUrlFromAss(String name) {
        String picurl = "assets://" + name;
        return picurl;
    }

    public static String getUrlDrawables(int name) {
        String picurl = "drawable://" + name;
        return picurl;
    }

    public static String getUrlSD(String name) {
        String picurl = "file://" + name;
        return picurl;
    }
}
